package scm.inventoryControl.managedbean;

import commonInfrastructure.accountManagement.entity.EmployeeEntity;
import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;
import commonInfrastructure.internalMessaging.session.MessageSessionBean;
import crm.salesOrderProcessing.entity.DeliveryOrderEntity;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import scm.inventoryControl.entity.PackingOrderEntity;
import scm.inventoryControl.entity.PickingOrderEntity;
import scm.inventoryControl.entity.ShippingOrderEntity;
import scm.inventoryControl.session.stateless.InventoryControlSessionBean;

@ManagedBean
@ViewScoped
public class GenerateOrdersManagedBean implements Serializable {

    @EJB
    private InventoryControlSessionBean inventorySessionBean;
    @EJB
    private MessageSessionBean messageSessionBean;
    @EJB
    EmployeeSessionBean employee;
    // private DeliveryOrderEntity selectedDeliveryOrder;
    private DeliveryOrderEntity selectedForView;
    private PickingOrderEntity pickingOrder;
    private PackingOrderEntity packingOrder;
    private ShippingOrderEntity shippingOrder;

    public GenerateOrdersManagedBean() {
        pickingOrder = new PickingOrderEntity();
        packingOrder = new PackingOrderEntity();
        shippingOrder = new ShippingOrderEntity();

    }

    public List<DeliveryOrderEntity> getDeliveryOrders() {
        return inventorySessionBean.getDeliveryOrders();
    }

    public void generateOrders(DeliveryOrderEntity selectedDeliveryOrder) throws IOException {

        //setSelectedDeliveryOrder((DeliveryOrderEntity)event.getComponent().getAttributes().get("selectedDO"));
        //System.out.println("%%%%%%%%%%%%"+getSelectedDeliveryOrder().getId()); 
        //generate picking Order
        selectedDeliveryOrder.setIsShipped(true);
        pickingOrder.setGenerateDate(Calendar.getInstance().getTime());

        pickingOrder.setDeliveryOrder(selectedDeliveryOrder);
        selectedDeliveryOrder.setPickingOrder(pickingOrder);
        // inventorySessionBean.savePickingOrder(pickingOrder);
        //generate packing order
        packingOrder.setGenerateDate(Calendar.getInstance().getTime());
        packingOrder.setDeliveryOrder(selectedDeliveryOrder);
        // inventorySessionBean.savePackingOrder(packingOrder);
        selectedDeliveryOrder.setPackingOrder(packingOrder);
        //generate shipping order
        //shippingOrder.setGenerateDate(Calendar.getInstance().getTime());
        shippingOrder.setDeliveryOrder(selectedDeliveryOrder);
        //inventorySessionBean.saveShippingOrder(shippingOrder);
         selectedDeliveryOrder.setShippingOrder(shippingOrder);
        inventorySessionBean.savePickingOrder(pickingOrder);
        inventorySessionBean.savePackingOrder(packingOrder);
        inventorySessionBean.saveShippingOrder(shippingOrder);
        inventorySessionBean.saveDeliveryRelationship(selectedDeliveryOrder);
        //
        //FacesContext.getCurrentInstance().getExternalContext().getFlash().put("salesOrder", selectedSalesOrder);
        sendMessageToSalesDepartment(selectedDeliveryOrder);

        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedDeliveryOrder", selectedDeliveryOrder);
        FacesContext.getCurrentInstance().getExternalContext().redirect("generateOrdersResult.xhtml");

    }

    private void sendMessageToSalesDepartment(DeliveryOrderEntity selectedDeliveryOrder) throws IOException {
        try {
            SalesmanEntity salesmanReceiver = selectedDeliveryOrder.getParentSalesOrder().getConfirmedBy();
            String type = "Private";
            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            Long senderId = (Long) request.getSession().getAttribute("userId");
            EmployeeEntity user = employee.getEmployeeById(senderId);
            List<String> userType = new ArrayList<String>();

            for (int i = 0; i < user.getRoles().size(); i++) {
                userType.add(user.getRoles().get(i).getRoleName());
            }
            String title="Product Shipping Confirmation";
            String msg="Products on Delivery Order "+selectedDeliveryOrder.getId() + " has been shipped Out.";


            messageSessionBean.addPrivateMessage(senderId, salesmanReceiver.getId(), title, msg, type);
        } catch (Exception e) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "No such person", ""));
            return;
        }
    }

    public DeliveryOrderEntity getSelectedForView() {
        return selectedForView;
    }

    public void setSelectedForView(DeliveryOrderEntity selectedForView) {
        this.selectedForView = selectedForView;
    }

    public PickingOrderEntity getPickingOrder() {
        return pickingOrder;
    }

    public void setPickingOrder(PickingOrderEntity pickingOrder) {
        this.pickingOrder = pickingOrder;
    }

    public PackingOrderEntity getPackingOrder() {
        return packingOrder;
    }

    public void setPackingOrder(PackingOrderEntity packingOrder) {
        this.packingOrder = packingOrder;
    }

    public ShippingOrderEntity getShippingOrder() {
        return shippingOrder;
    }

    public void setShippingOrder(ShippingOrderEntity shippingOrder) {
        this.shippingOrder = shippingOrder;
    }
}
